﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Inventory
{
   public  class ClearControls
    {
        public static void ClearControl(Control controlName)
        {
            for (int index = 0; index < controlName.Controls.Count; index++)
            {
                // 如果是容器内部控件，则递归调用自己
                if (controlName.Controls[index].HasChildren)
                {
                    ClearControl(controlName.Controls[index]);
                }
                else
                {
                    switch (controlName.Controls[index].GetType().Name)
                    {
                        case "TextBox":
                            (controlName.Controls[index] as TextBox).Text = "";
                            break;
                        case "RadioButton":
                            (controlName.Controls[index] as RadioButton).Checked = false;
                            break;
                        case "CheckBox":
                            (controlName.Controls[index] as CheckBox).Checked = false;
                            break;
                        case "ComboBox":
                            (controlName.Controls[index] as ComboBox).Text = "";
                            (controlName.Controls[index] as ComboBox).SelectedIndex = -1;
                            break;
                        case "DataGridView":
                            (controlName.Controls[index] as DataGridView).DataSource = null;
                            break;
                        case "ListBox":
                            (controlName.Controls[index] as ListBox).Items.Clear();
                            break;
                        case "DateTimePicker":
                            (controlName.Controls[index] as DateTimePicker).Checked = false;
                            break;
                    }
                }
            }
        }
    }
}
